<?php

namespace App\Http\Controllers\Account;

use App\Http\Controllers\Admin\BaseController;
use App\Models\Config;
use App\Models\Shop;

/**
 * 后台首页控制器
 */
class LoginController extends BaseController
{

    public function __construct(Shop $shop, Config $config)
    {
        parent::__construct();
        $this->shop = $shop;
        $this->config = $config;
    }

    public function getIndex()
    {
        if (is_account_login()) {
            $url = urlAccount('single','getStatistic');
            return redirect($url);
        }
        return view('admin.login');
    }

    /**
     * 后台用户登录
     */
    public function postIndex()
    {
        // 检测验证码
//        if (!check_verify(request('verify'))) {
//            $this->error('验证码输入错误！');
//        }
        $res = $this->shop->login();
        if (!$res) {
            return $this->error($this->shop->getError());
        }
        $url = urlAccount('single','getStatistic');

        return $this->success('登录成功！', $url);
    }


    /* 退出登录 */
    public function getLogout()
    {
        if (session('account')) {
            session()->flush();
            return $this->success('退出成功！', urlAdmin('login', 'getIndex'));
        } else {
            return redirect(urlAdmin('login', 'getIndex'));
        }
    }
}
